﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using Bestlife.ERP.DataAccess;
using System.Text;

public partial class Web_levelGen : System.Web.UI.Page
{
    SqlDataReader rdRStatus;


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public Boolean level_users() 
    {

        Bestlife.ERP.BusinessRules.root_level rl = new Bestlife.ERP.BusinessRules.root_level();
        string index = rl.root_levelID();

        List<string> child = new List<string>();
        child.Clear();
        SqlConnection conn_new = new SqlConnection();
        String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
        Bestlife.ERP.DataAccess.clsCon.connect(connStr);

        String sqlstatus = "SELECT User_id from Distributor_Level where Parent_To_User='" + index + "'";

        rdRStatus = clsCon.ReadTable(sqlstatus);

        while (rdRStatus.Read())
        {
            child.Add(rdRStatus["User_id"].ToString());
        }
        rdRStatus.Close();

        process(child);

        return true;
    }

    public Boolean process(List<string> listset) 
    {

        List<int> childset = new List<int>();
        
        try
        {

            foreach(string i in listset)
            {
                ////int i = 1018;

                childset.Clear();
                string sup_id = i.ToString();

                SqlConnection conn_new = new SqlConnection();
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String sqlstatus = "SELECT Dis_ID from Tbl_Distributor where Sup_ID='" + sup_id + "'";

                rdRStatus = clsCon.ReadTable(sqlstatus);

                while (rdRStatus.Read())
                {
                    childset.Add(int.Parse(rdRStatus["Dis_ID"].ToString()));
                }
                rdRStatus.Close();

                Bestlife.ERP.DataAccess.D_Level_Genarator lgn = new D_Level_Genarator();
                string rvalue = lgn.Level_Gen(sup_id);

                string[] arr = rvalue.Split('/');
                string Parent_To_User = arr[0].ToString();
                string Child_Level = arr[1].ToString();

                childset.Sort();
                int point = 0;
                foreach (int child in childset)
                {
                    point++;

                    string childid = child.ToString();
                    int parent_to_child = int.Parse(Parent_To_User) + 1;
                    string clevel = Child_Level + "." + point;


                    try
                    {
                        SqlConnection conn_news = new SqlConnection();
                        String connSt = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                        String Ssql = "INSERT INTO Distributor_Level(User_id,Parent_To_User,Level_id,Date_Create,Last_Modify)VALUES('" + childid + "','" + parent_to_child + "','" + clevel + "','" + DateTime.Now + "','" + DateTime.Now + "')";
                        Boolean exec = Bestlife.ERP.DataAccess.clsCon.AddEditDel(Ssql);
                    }
                    catch (Exception)

                    { }
                }
            }

            level_users();

        }
        catch (Exception)
        {

        }

        return true;

    }







    protected void Button1_Click(object sender, EventArgs e)
    {

        level_users();
       
    
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        StringBuilder result = new StringBuilder();
        result.AppendLine("<ul>");
        List<int> listint = new List<int>();
         SqlConnection conn_new = new SqlConnection();
            String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
            Bestlife.ERP.DataAccess.clsCon.connect(connStr);

            String sqlstatus = "SELECT [User_id] ,[Parent_To_User],[Level_id],[Date_Create],[Last_Modify] ,[Key_Timestamp]FROM [BestlifeDB].[dbo].[Distributor_Level]";

            rdRStatus = clsCon.ReadTable(sqlstatus);

            while (rdRStatus.Read())
            {
                listint.Add(int.Parse(rdRStatus["User_id"].ToString()));
            }
            rdRStatus.Close();

            listint.Sort();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        //string disid = "";
        //string name = "";

        //addBonusDetails(disid,name);
    }

    //public static int addBonusDetails(string Disid,string name)
    //{
        //SqlConnection conn_new = new SqlConnection();
        //int status = 0;
        //try
        //{
        //    String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
        //    Bestlife.ERP.DataAccess.clsCon.connect(connStr);

        //    String Ssql = "INSERT INTO Bonus(User_id,Tpv,ppv,Cpv,CPV_month,tppv_month,T_sales,T_commision,T_Lship,Bv,user_Status,Lship_Status,Tbv,tbv_month,User_Create,Date_Create,Last_Modify,Store_pv,Store_bv,Store_comm,Store_Lship)VALUES('" + Disid + "','0','0','0','0','0','0','0','0','0','0','0','0','0','" + name + "','" + DateTime.Now + "','" + DateTime.Now + "','0','0','0','0')";
        //    Boolean exec = Bestlife.ERP.DataAccess.clsCon.AddEditDel(Ssql);
        //}
        //catch (Exception)
        //{
        //    // Console.WriteLine(exx);
        //}
        //return status;
    //}
}
